package com.linran.structure_algorithm.数据结构.lesson2_稀疏数组;

import java.io.*;

/**
 * 原始二维数组
 *
 * @author lin
 * @date 2020-08-30 20:27
 */
public class SparseArray {
    public static void main(String[] args) {
        //创建一个原始的二维数组11*11
        //0:表示没有棋子,1:表示黑子,2:表示蓝子
        int chessArra1[][] = new int[11][11];
        chessArra1[1][2] = 1;
        chessArra1[2][3] = 2;
        //输出原始二维数组
        for (int[] row : chessArra1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }


        //将二维数组转稀疏数组
        //1.先遍历二维数组，得到非0数据的个数
        int sum = 0;
        for (int i = 0; i < chessArra1.length; i++) {
            for (int j = 0; j < chessArra1[i].length; j++) {
                if (chessArra1[i][j] != 0) {
                    sum++;
                }
            }
        }

        //2.创建对应的稀疏数组
        int sparseArr[][] = new int[sum+1][3];
        //给稀疏数组赋值
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;

        //遍历二维数组，将非0的数据写入稀疏数组
        int count=0; //count 用于记录时第几个非0数据
        for (int i = 0; i < chessArra1.length; i++) {
            for (int j = 0; j < chessArra1[i].length; j++) {
                if (chessArra1[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArra1[i][j];
                }
            }
        }

        //输出稀疏数组
        System.out.println();
        System.out.println("得到稀疏数组为~~~~~");
        for (int i = 0; i< sparseArr.length; i++) {
            System.out.printf("%d\t%d\t%d\t\n", sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]);
        }
        System.out.println();

        wrieFile(sparseArr);
        int[][] ints = readFile();

        //将稀疏数组恢复成原始的二维数组
        System.out.println();
        int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
        //输出恢复后的二维数组
        //在读取稀疏数组后几行的数据(从第二行开始),并赋给原始的二维数组即可
        for (int i = 1; i < sparseArr.length; i++) {
            chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }
        //输出
        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
    }

    private static void wrieFile(int sparseArr[][]) {
        File file = new File("E:\\java资料\\代码\\lin_module\\structure_algorithm\\src\\main\\java\\com\\linran\\structure_algorithm\\数据结构\\data.map");
        try (FileWriter writer = new FileWriter(file)) {
            for (int[] ints : sparseArr) {
                StringBuilder sb = new StringBuilder();
                for (int anInt : ints) {
                    sb.append(anInt).append("|");
                }
                sb.deleteCharAt(sb.lastIndexOf("|"));
                sb.append("\r\n");
                writer.write(sb.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static int[][] readFile() {
        int line = 0;
        int row = 0;
        File file = new File("E:\\java资料\\代码\\lin_module\\structure_algorithm\\src\\main\\java\\com\\linran\\structure_algorithm\\数据结构\\data.map");
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String buffer = null;
            while ((buffer = reader.readLine()) != null) {
                String[] split = buffer.split("\\|");
                line = split.length;
                row++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        int[][] ints = new int[row][line];
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            int count = 0;
            String buffer = null;
            while ((buffer = reader.readLine()) != null) {
                String[] split = buffer.split("\\|");
                for (int i = 0; i < split.length; i++) {
                    ints[count][i] = Integer.valueOf(split[i]);
                }
                count++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ints;
    }
}
